layui.use(['jquery'], function () {
    const $ = layui.jquery;

    let loginUserCode = localStorage.getItem("loginUserCode");
    let result = commonAjax(AUTH_SERVER_URL+"/user/findByCode", {code: loginUserCode}, "get");
    setUser(result.data);

    //获取用户菜单权限
    let menu = commonAjax(AUTH_SERVER_URL+"/permission/findUserMenuPermission", {userCode: loginUserCode}, "get");
    setMenu(menu.data.list);

    /**
     * 设置登录用户信息
     * @param data
     */
    function setUser(data) {
        $("#loginUser").html(data.username);
        if (data.avatar !== null && data.avatar !== undefined && data.avatar !== '') {
            $('#userAvatar').attr('src', data.avatar);
        }
    }

    /**
     * 根据用户权限动态生成菜单
     * @param data
     */
    function setMenu(data) {
        let ulHtml = '<ul class="layui-nav layui-nav-tree ">';
        for (let i = 0; i < data.length; i++) {
            ulHtml += '<li class="layui-nav-item">';
            if (data[i].children !== undefined && data[i].children !== null && data[i].children.length > 0) {
                ulHtml += '<a href="javascript:;" target="option">' + data[i].title;
                ulHtml += '<span class="layui-nav-more"></span>';
                ulHtml += '</a>';
                ulHtml += '<dl class="layui-nav-child">';
                //二级菜单
                for (let j = 0; j < data[i].children.length; j++) {
                    //是否有孙子节点
                    if (data[i].children[j].children !== undefined && data[i].children[j].children !== null && data[i].children[j].children.length > 0) {
                        ulHtml += '<dd>';
                        ulHtml += '<a href="javascript:;" target="option">' + data[i].children[j].title;
                        ulHtml += '<span class="layui-nav-more"></span>';
                        ulHtml += '</a>';
                        //三级菜单
                        ulHtml += '<dl class="layui-nav-child">';
                        let grandsonNodes = data[i].children[j].children;
                        for (let k = 0; k < grandsonNodes.length; k++) {
                            ulHtml += '<dd>';
                            ulHtml += '<a href="' + grandsonNodes[k].href + '" target="option">' + grandsonNodes[k].title + '</a>';
                            ulHtml += '</dd>';
                        }
                        ulHtml += '</dl>';
                        ulHtml += '</dd>';
                    } else {
                        ulHtml += '<dd>';
                        ulHtml += '<a href="' + data[i].children[j].href + '" target="option">' + data[i].children[j].title;
                        ulHtml += '</a>';
                        ulHtml += '</dd>';
                    }
                }
                ulHtml += '</dl>';
            }
            ulHtml += '</li>';
        }
        ulHtml += '</ul>';

        $('#nav-menu').html(ulHtml);
    }

    /**
     * 退出登录
     */
    $('#exitLogin').bind('click', function () {
        localStorage.setItem("tokenHead", '');
        localStorage.setItem("token",'');
        localStorage.setItem("loginUserCode",'');
        window.location.href='../../login.html';
    })
});